package com.example.appxi;
import android.app.Activity;
import android.widget.Toast;
 
public class BackPressCloseHandler {
 
    private long backKeyPressedTime = 0;
    private Toast toast;
    
 
    private Activity activity;
 
    public BackPressCloseHandler(Activity context) {
        this.activity = context;
    }
 
    public void onBackPressed(Activity top) {
    	if(top.getClass()!=MainActivity.class)
    	{activity.finish();
        }
    	else if (System.currentTimeMillis() > backKeyPressedTime + 2000) {
            backKeyPressedTime = System.currentTimeMillis();
            
            showGuide();
            return;
        }
        if (System.currentTimeMillis() <= backKeyPressedTime + 2000) {
        	toast.cancel();
        	activity.moveTaskToBack(true);
        	activity.finish();
        	android.os.Process.killProcess(android.os.Process.myPid());
        	System.exit(0);

        	
        	
        }
    }
 
    public void showGuide() {
        toast = Toast.makeText(activity,
                "\'뒤로\'버튼을 한번 더 누르시면 종료됩니다.", Toast.LENGTH_SHORT);
        toast.show();
    }
    
    
}